﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace KIS公有云测试.Tools
{
    public class AuthResponse<T>
    {
        public int ResCode { get; set; }
        public string ResMsg { get; set; }
        public List<T> Data { get; set; } // This will contain the token
    }
    public class AuthService
    {
        private readonly string _tokenUrl;

        /// <summary>
        /// 获取Token方法
        /// </summary>
        /// <param name="clientId"></param>
        /// <param name="clientSecret"></param>
        /// <param name="entCode"></param>
        /// <param name="userCode"></param>
        /// <param name="orgCode"></param>
        /// <returns></returns>
        public string GetToken(string clientId, string clientSecret, string entCode, string userCode, string orgCode)
        {
            var requestUri = $"{_tokenUrl}?clientid={clientId}&clientsecret={clientSecret}&entCode={entCode}&userCode={userCode}&orgCode={orgCode}";

            try
            {
                using (var webClient = new WebClient())
                {
                    webClient.Encoding = Encoding.UTF8;
                    webClient.Headers.Add("Content-Type", "application/json");
                    webClient.Headers.Add("Accept", "*/*");
                    var responseContent = webClient.DownloadString(requestUri);
                    var authResponse = JsonConvert.DeserializeObject<dynamic>(responseContent);

                    if (authResponse.ResCode == 0)
                    {
                        return authResponse.Data;
                    }
                    else
                    {
                        throw new Exception($"Failed to get token: {authResponse.ResMsg}");
                    }
                }
            }
            catch (WebException e)
            {
                throw new Exception($"Request error: {e.Message}");
            }
        }
        /// <summary>
        /// Post方法调用Uri
        /// </summary>
        /// <typeparam name="T">自定义返回类型</typeparam>
        /// <param name="requestUri"></param>
        /// <param name="token"></param>
        /// <param name="jsonBody"></param>
        /// <returns></returns>
        public string AccessUrlByPOST<T>(string requestUri, Dictionary<string,string> dic, string jsonBody)
        {
            var responseContent = "";
            try
            {
                using (var webClient = new WebClient())
                {
                    // 获取当前UTC时间的秒级时间戳
                    
                    webClient.Encoding = Encoding.UTF8;
                    webClient.Headers.Add("Content-Type", "application/json");
                    webClient.Headers.Add("Accept", "*/*");
                    // 标准写法
                    foreach (KeyValuePair<string, string> kvp in dic)
                    {
                        webClient.Headers.Add(kvp.Key, kvp.Value);
                        
                    }
                
                    responseContent = webClient.UploadString(requestUri, "POST", jsonBody);
                    ConcurrentLogger.Log(LogLevel.Debug, responseContent, this.GetType().Name, "Create");
                    return responseContent;
                    //var authResponse = JsonConvert.DeserializeObject<AuthResponse<T>>(responseContent);
                    //if (authResponse.ResCode == 0)
                    //{
                    //    return responseContent;
                    //}
                    //else
                    //{
                    //    throw new Exception($"Failed to create item master: {authResponse.ResMsg}");
                    //}
                }
            }
            catch (WebException e)
            {
                throw new Exception($"Request error: {responseContent}");
            }
        }
    }
}